package com.ltsh.fim.server;

import com.ltsh.fim.server.inf.Section;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * Created by root on 1/11/18.
 */
public class ChineseProverbServer {
    private Section section;
    public void run(int port) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
                try {
                    Bootstrap bootstrap = new Bootstrap();
                    bootstrap.group(eventLoopGroup).channel(NioDatagramChannel.class)
                            .option(ChannelOption.SO_BROADCAST, true)
                            .handler(new ChineseProverbServerHandler(section));

                    bootstrap.bind(port).sync().channel().closeFuture().sync();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    eventLoopGroup.shutdownGracefully();
                }
            }
        }).start();

    }
    public ChineseProverbServer(Section section) {
        this.section = section;
    }
//
//    public static void main(String[] args) {
//        ChineseProverbServer server = new ChineseProverbServer();
//        server.run(8080);
//    }
}